<?php

namespace logInController;

require_once 'view/LogInPage.php';
require_once 'view/Bootstraphtml.php';
require_once 'model/LogInModel.php';
require_once 'controller/PostController.php';
require_once 'model/CreateComments.php';

class LogInController{
	
	private $model;
	private $viewlogInPage;
	private $viewboothtml;
	private $LoggedInStatus;
	private $createComments;		

	public function __construct(){
		$this->postController = new \postController\PostController();
		$this->model = new \logInModel\LogInModel();
		$this->viewlogInPage = new \logInPage\LogInPage();
		$this->viewboothtml = new \bootstraphtml\Bootstraphtml();
		$this->createComments = new \createComments\CreateComments();
	}		
	
	//@return string (html)
	public function control(){
		$this->checkButtons();
		$body = $this->getBody();
		$table = $this->postController->printControl();
			
		$html = $this->viewboothtml->getbootstraphtml($body, $table);
		return $html;
	}
	
	//@return string (body html)
	private function getBody(){
		$message = $this->model->getMessage();
		//$this->viewlogInPage->setMessage($message);
		if($this->model->checkIfLoggedIn() == TRUE){
			$usernameSession = $this->model->getUsername();
            $header = $this->viewlogInPage->loggedInHeader($usernameSession);
			$page = $this->viewlogInPage->getLoggedInPage($message);
		}else{
			$header = $this->viewlogInPage->loggedOutHeader();
			$page = $this->viewlogInPage->getLoggedOutPage($message);
		}
		
		$body = $header . $page; 
		
		return $body;
	}
	
	//checks what button is pressed
	private function checkButtons(){
		if($this->viewlogInPage->registrerButton() == TRUE){
			$this->doRegistrer();
		}else if($this->viewlogInPage->logInButton() == TRUE){
			$this->loginControlChecker();
		}else if ($this->viewlogInPage->logOutButton() == TRUE){
			$this->model->logOut();
		}else if($this->viewlogInPage->submitPicButton()){
			$this->postController->postControl();
		}else if($this->viewlogInPage->commentButton()){
			$this->postController->createComments();
		}else if($this->viewlogInPage->deletCommentBtn()){
			$this->postController->deleteComments();
		}else if($this->viewlogInPage->getEditButton()){
			$this->postController->editComment();
		}
		
	}
	
	//gets usern name and password then tries to log in in model
	private function loginControlChecker(){
		$username = $this->viewlogInPage->getUsername();
		$password = $this->viewlogInPage->getPassword();
		$this->LoggedInStatus = $this->model->tryLogIn($username, $password);
	}
	
	//gets usern name and password then tries to register new user in model
	private function doRegistrer(){
			$username = $this->viewlogInPage->getUsername();
			$password = $this->viewlogInPage->getPassword();
			$this->model->addUser($username, $password);	
	}
}
